﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BaseHelper;
using BaseHelper.Base;

namespace DeviceSDK.LED.LingXin
{
    public class LingXin_E
    {
        private static SortedList<string, LingXin_ESDK.COMMUNICATIONINFO> slIP = new SortedList<string, LingXin_ESDK.COMMUNICATIONINFO>();
        private static int ledColor = 2;

        private static object locker = new object();
        private static bool Sending = false;

        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";
                case 1:
                    return "操作失败！";
                case 2:
                    return "初始化设备出错！";

                case -1:
                    return "本设备不支持此命令！";
                default:
                    return LingXin_ESDK.LS_GetError(ret);
            }
        }

        public static int Test(string IP)
        {
            bool ret = Connect(IP);
            if (!ret)
                return 2;

            LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];

            return LingXin_ESDK.LV_TestOnline(ref led);
        }

        public static bool Connect(string IP)
        {
            if(slIP.ContainsKey(IP))
                return true;

            int nResult;
            LingXin_ESDK.COMMUNICATIONINFO led = new LingXin_ESDK.COMMUNICATIONINFO();//定义一通讯参数结构体变量用于对设定的LED通讯，具体对此结构体元素赋值说明见COMMUNICATIONINFO结构体定义部份注示
            //ZeroMemory(&CommunicationInfo,sizeof(COMMUNICATIONINFO));
            //TCP通讯********************************************************************************
            led.SendType = 0;//设为固定IP通讯模式，即TCP通讯
            led.IpStr = IP;//给IpStr赋值LED控制卡的IP
            led.LedNumber = 1;//LED屏号为1，注意socket通讯和232通讯不识别屏号，默认赋1就行了，485必需根据屏的实际屏号进行赋值
            //led.LEDType = 0;//


            //nResult = LingXin_ESDK.LV_SetBasicInfo(ref led, ledColor, Width, Height);//设置屏参，屏的颜色为2即为双基色，64为屏宽点数，32为屏高点数，具体函数参数说明见函数声明注示
            nResult = LingXin_ESDK.LV_TestOnline(ref led);

            if (nResult == 0)
            {
                slIP[IP] = led;
                return true;
            }

           // Console.WriteLine("LingXin_E.cs -> " + LingXin_ESDK.LS_GetError(nResult));

            return false;

            
            
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP)
        {
            bool ret = Connect(IP);
            if (!ret)
                return 2;

            LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];

            return LingXin_ESDK.LV_PowerOnOff(ref led,0);
        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP)
        {
            bool ret = Connect(IP);
            if (!ret)
                return 2;

            LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];

            return LingXin_ESDK.LV_PowerOnOff(ref led, 1);
        }


        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Value)
        {
            bool ret = Connect(IP);
            if (!ret)
                return 2;

            Value = (int)(((double)Value / 100.0f)*15.0f);
            Value=Value>15?0:15-Value;
           
            LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];
            
            return LingXin_ESDK.LV_SetBrightness(ref led, Value);

        }

        //设置-时间
        public static int Set_Time(string IP)
        {
            bool ret = Connect(IP);
            if (!ret)
                return 2;
            LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];

            return LingXin_ESDK.LV_AdjustTime(ref led);
        }

        //发送-节目
        public static int Send_Programs(string IP, int Width, int Height, PlaybillProgramInfo[] Programs)
        {
            int ret = 0;
            int hProgram=0;//节目句柄

            
            if (LEDHelper.Release_IsOrderToSend)
            {
                Random rnd = new Random();
                while ((bool)Sending)
                {
                    Thread.Sleep(rnd.Next(500, 1000));
                }
            }

            lock (locker)
            {
                Sending = true;
                try
                {
                    if (!Connect(IP))
                    {
                        Sending = false;
                        return 2;
                    }

                    LingXin_ESDK.COMMUNICATIONINFO led = slIP[IP];

                    hProgram = LingXin_ESDK.LV_CreateProgram(Width, Height, ledColor);//根据传的参数创建节目句柄，64是屏宽点数，32是屏高点数，2是屏的颜色，注意此处屏宽高及颜色参数必需与设置屏参的屏宽高及颜色一致，否则发送时会提示错误
                    //此处可自行判断有未创建成功，hProgram返回NULL失败，非NULL成功,一般不会失败

                    ret = LingXin_ESDK.LV_AddProgram(hProgram, 1, 0, 1);//添加一个节目，参数说明见函数声明注示

                    for (int i = 0; i < Programs.Length;i++ )
                    {
                        PlaybillProgramInfo pinfo = Programs[i];
                        LingXin_ESDK.AREARECT AreaRect = new LingXin_ESDK.AREARECT();//区域坐标属性结构体变量
                        AreaRect.left = pinfo.X;
                        AreaRect.top = pinfo.Y;
                        AreaRect.width = pinfo.Width;
                        AreaRect.height = pinfo.Height;

                        LingXin_ESDK.LV_AddImageTextArea(hProgram, 1, 1, ref AreaRect, 0);

                        LingXin_ESDK.PLAYPROP PlayProp = new LingXin_ESDK.PLAYPROP();
                        PlayProp.InStyle = 0;
                        PlayProp.OutStyle = 0;
                        PlayProp.DelayTime = pinfo.PlayTime;
                        PlayProp.Speed = 0;

                        LingXin_ESDK.FONTPROP FontProp;

                        switch (pinfo.Type)
                        {
                            case "picture":
                                ret = LingXin_ESDK.LV_AddFileToImageTextArea(hProgram, 1, 1, pinfo.FilePath, ref PlayProp);
                                break;

                            case "video":
                                continue;
                                break;

                            case "rtf":
                                FontProp = new LingXin_ESDK.FONTPROP();//文字属性
                                FontProp.FontName = pinfo.FontName;
                                FontProp.FontSize = pinfo.FontSize;

                                if (pinfo.FontColor == "#FF0000")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_RED;
                                else if (pinfo.FontColor == "#00FF00")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_GREEN;
                                else if (pinfo.FontColor == "#FFFF00")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_YELLOW;
                                else
                                    FontProp.FontColor = LingXin_ESDK.COLOR_RED;

                                FontProp.FontBold = pinfo.FontIsBold ? 1 : 0;

                                ret = LingXin_ESDK.LV_AddMultiLineTextToImageTextArea(hProgram, 1, 1, LingXin_ESDK.ADDTYPE_FILE, pinfo.FilePath, ref FontProp, ref PlayProp, pinfo.PositionHor, pinfo.PositionVer == 2 ? 1 : 0);//通过rtf文件添加一个多行文本到图文区，参数说明见声明注示


                                break;

                            case "text":
                                ////2020.5.20 
                                //pinfo.Type = "picture";
                                //pinfo.FilePath = TextToImage.CreateImage(LEDHelper.TempFullPath, pinfo.Text, pinfo.Width, pinfo.Height, pinfo.FontName, pinfo.FontSize, pinfo.FontColor);
                                //i--;
                                //continue;


                                FontProp = new LingXin_ESDK.FONTPROP();//文字属性
                                FontProp.FontName = pinfo.FontName;
                                FontProp.FontSize = pinfo.FontSize - 16;  //2020.1.10 荆州公安局诱导屏测试，32号字实际为42号字

                                if (pinfo.FontColor == "#FF0000")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_RED;
                                else if (pinfo.FontColor == "#00FF00")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_GREEN;
                                else if (pinfo.FontColor == "#FFFF00")
                                    FontProp.FontColor = LingXin_ESDK.COLOR_YELLOW;
                                else
                                    FontProp.FontColor = LingXin_ESDK.COLOR_RED;

                                FontProp.FontBold = pinfo.FontIsBold ? 1 : 0;

                                ret = LingXin_ESDK.LV_AddMultiLineTextToImageTextArea(hProgram, 1, 1, LingXin_ESDK.ADDTYPE_STRING, pinfo.Text, ref FontProp, ref PlayProp, pinfo.PositionHor, pinfo.PositionVer == 2 ? 1 : 0);//通过字符串添加一个多行文本到图文区，参数说明见声明注示

                                break;
                        }
                    }


                    ret = LingXin_ESDK.LV_Send(ref led, hProgram);//发送，见函数声明注示
                    LingXin_ESDK.LV_DeleteProgram(hProgram);//删除节目内存对象，详见函数声明注示
                }
                catch (Exception ex)
                {
                    LogHelper.Err("RT.Helper.Device.LED.LingXin.LingXin_E.cs", "Send_Programs()", ex);
                    return 1;
                }
                finally
                {
                    Sending = false;
                }
            }

            return ret;
        }

    }
}
